package cn.tedu.suppermemory.controller;

import cn.tedu.suppermemory.pojo.dto.UserRoleAddNewDTO;
import cn.tedu.suppermemory.pojo.vo.UserRoleListItemVO;
import cn.tedu.suppermemory.service.IRoleService;
import cn.tedu.suppermemory.service.IUserRoleService;
import cn.tedu.suppermemory.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理用户角色相关请求的控制器类
 *
 * @author xuchuang
 * @version 0.0.10
 */
@Api(tags = "0052. 用户角色管理模块")
@Slf4j
@RestController
@RequestMapping("/userroles")
@Validated
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRoleService roleService;

    public UserRoleController() {
        log.info("创建控制器类对象:userroleService");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加用户角色")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(UserRoleAddNewDTO userRoleAddNewDTO) {
        log.info("开始处理【添加用户角色】的请求，参数：{}", userRoleAddNewDTO);
        userRoleService.addNew(userRoleAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8888/userroles/9527/delete
    @PostMapping("/{id:[0-9]+/delete")
    @ApiOperation("删除用户角色")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@Range(min = 1, max = 100000, message = "删除用户角色失败！用户角色id非法！")
                                   @PathVariable Long id) {
        log.info("开始处理【根据id删除用户角色】的请求，参数：{}", id);
        roleService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8888/userroles
    @GetMapping("")
    @ApiOperation("查询用户角色列表")
    @ApiOperationSupport(order = 300)
    public JsonResult<List<UserRoleListItemVO>> list() {
        log.info("开始处理【查询用户角色列表】的业务，无参数");
        List<UserRoleListItemVO> list = userRoleService.list();
        return JsonResult.ok(list);
    }


}
